#Copyright 2009-2010 by Ward, David J ( pymethius@ominian.net )
import logging
log = logging.getLogger(__name__)

from ..util.reloadablehandlers import ReloadableHandlers
from twisted.python.rebuild import Sensitive

from webmud.lib.dbfactory import DBFactory

class Base(Sensitive):
    
    def __init__(self, environ, *a,**kw):
        
        
        self._assignEnviron(environ)        
        self.db = DBFactory
        
        self.initialize()
        
        ReloadableHandlers.reloader(self.__module__)
    
    def __call__(self, environ):
        """
            Because a user can disconnect and reconnect to accounts, ocassionally some issues crop up where
            a reference to the old protocol still exists
        """
        self._assignEnviron(environ)
        return self;
        
    def _assignEnviron(self, environ):
        self.environ = environ
        
        for key in environ.keys():
            setattr(self, key, environ[key])
                    
        self.makeRequest = environ["protocol"].makeRequest
        self.sendResponse = environ["protocol"].sendResponse
        self.sendRequest  = environ["protocol"].sendRequest
        self.sendError = environ["protocol"].sendError
        
        
        
    
    def makeRequest(*args, **kwargs):
        self.environ
    
    def initialize(self):
        """A stub function to allow handlers to start themselves up without interferring with __init__"""
        
    def _callback(self, _action, _data , **kwargs):
        _handler = kwargs.get("handler", self.__reg_name__)
        return dict(handler = _handler, action = _action, data = _data )
    
    def _success(self, **msg):
        msg["success"] = True                     
        return msg
            
    